﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MoneyManagerNET.Presentation.Infrastructure.Collections;
using System.Windows.Data;
using MoneyManagerNET.Presentation.Infrastructure;
using System.Windows.Input;
using System.Collections;

namespace MoneyManagerNET.Presentation.ViewModel.Base
{


    //for list based windows
    //Model property returns currently selected (may be this is not right)
    public class ListViewModel : ViewModelBase 
    {

        CollectionView _itemsView;
        public CollectionView ItemsView
        {
            get { return _itemsView; }
            set
            {
                if (_itemsView != null)
                    _itemsView.CurrentChanged -= _itemsView_CurrentChanged;
                _itemsView = value;
                if (_itemsView != null)
                    _itemsView.CurrentChanged += _itemsView_CurrentChanged;
            }
        }

        void _itemsView_CurrentChanged(object sender, EventArgs e)
        {
            Moved();
        }



        public object Selected {
            get { return ItemsView == null ? null : ItemsView.CurrentItem; }
            set { ItemsView.MoveCurrentTo(value);  }
        }

        protected virtual void Moved()
        {
            this.OnChange(()=>Selected);
        }

        public ListViewModel()
        {

        }

        public ListViewModel(IEnumerable source)
        {
            ItemsView = new CollectionView(source);

        }

        
    }
}
